/*
 ------------------------------------------------------------------------------
	sdlmedia - a test program for a media search library, including test UI.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#define _CRT_SECURE_NO_WARNINGS

#pragma once

#include <fcntl.h>
#include <stdio.h>

#ifdef WIN32

#include "targetver.h"
#include <tchar.h>
#include <windows.h>
#include <mmsystem.h>
#include <shlwapi.h>
#define usleep(x)	Sleep((DWORD)(x/1000))

#else

#define _strdup strdup
#define _strndup strndup
#define _stricmp stricmp

#endif

#define TRACE_THREAD		0x00000001
#define TRACE_DIR			0x00000002
#define TRACE_MEM			0x00000004
#define TRACE_LIB			0x00000008
#define TRACE_ID3			0x0000000F
#define TRACE_UI			0x00000010
#define TRACE_MP3			0x00000020
#define TRACE_FILTER (TRACE_DIR|TRACE_MEM|TRACE_LIB|TRACE_ID3|TRACE_UI|TRACE_MP3)

#ifdef _DEBUG
#ifdef WIN32
#define TRACE(msg, ...)	if (THIS_FILTER	& TRACE_FILTER) \
						{ \
							TCHAR szOutput[1024]; \
							_stprintf_s(szOutput, 1024, _T(msg), __VA_ARGS__); \
							OutputDebugString(szOutput); \
						}
#else
#define TRACE(msg, ...)	if (THIS_FILTER & TRACE_FILTER) \
						{ \
							printf(msg, __VA_ARGS__); \
						}
#endif
#else
#define TRACE(msg, ...)
#endif

/*
 ------------------------------------------------------------------------------
	Path utils

 ------------------------------------------------------------------------------
*/

inline const char* _path_find_extension(const char* pszPath)
{
#ifdef WIN32
	return PathFindExtensionA(pszPath);
#else
	return strrchr(pszPath, '.') + 1;
#endif
}

inline const char* _path_find_filename(const char* pszPath)
{
#ifdef WIN32
	return PathFindFileNameA(pszPath);
#else
	const char* p = strrchr(pszPath, '/');
	return ( (p == NULL) ? pszPath : p + 1 );
#endif
}

inline const char* _path_find_dir(const char* pszPath)
{

	char* pDir = (char*)malloc(strlen(pszPath)+1);
	if (pDir != NULL)
	{
		strcpy(pDir, pszPath);
#ifdef WIN32
		PathUnquoteSpacesA(pDir);
		if (!PathIsDirectoryA(pDir))
		{
			PathRemoveFileSpecA(pDir);
		}
#else
		const char* p = strrchr(pDir, '/');
		if (p != NULL)
		{
			*p = '\0';
		}
#endif
	}
	return pDir;
}

inline char* _path_extract_subfolder(const char* pszRoot, const char* pszPath, char* pszSubFolder, int nCharMax)
{
	const char* p = strstr(pszPath, pszRoot);
	const char* rel;
	size_t s;

	if (p == NULL)
	{
		rel = pszPath;
	}
	else
	{
		rel = p + strlen(pszRoot);
		if ( (*rel == '/') || (*rel == '\\') )
		{
			rel++;
		}
		p = strrchr(pszPath, '\\');
		if (p == NULL)
		{
			p = strrchr(pszPath, '/');
		}
		if (p != NULL)
		{
			s = p - rel;
			if (s > (size_t)nCharMax)
			{
				s = nCharMax;
			}
		}
		else
		{
			s = strlen(rel);
		}
		strncpy(pszSubFolder, rel, s);
		pszSubFolder[s] = '\0';
	}
	return pszSubFolder;
}